﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace JustSiteWorkrole.Helper
{
    public class SessionExpireFilterAttribute : System.Web.Mvc.ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext ctx = HttpContext.Current;
            // check if session is supported
            //here is excluded pages to in without login

            string urlPath = ctx.Request.Path.ToLower();
            if (ctx.Session["ID"] == null &&
                !(urlPath == @"/"
                || urlPath.Contains(@"login")
                ))
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary {{ "Controller", "Home" },
                                      { "Action", "Login" } });
            }
            else
                if (ctx.Session["ID"] != null && (int)ctx.Session["Role"] == (int)Role.User &&
                    (urlPath == @"/"
                    || urlPath == @"/home/admin"
                    || urlPath == @"/home/adduser"
                    || urlPath.Contains(@"login")
                    || urlPath == @"/home/statistika"
                    ))
                {
                    filterContext.Result = new RedirectToRouteResult(
                        new RouteValueDictionary {{ "Controller", "Home" },
                                      { "Action", "Index" } });
                }

            base.OnActionExecuting(filterContext);
        }
    }
}